package com.uuah.server.transport;

import org.apache.commons.pool.KeyedPoolableObjectFactory;

import com.uuah.config.i18n.CoreMessages;
import com.uuah.exception.AbstractUuahException;
import com.uuah.server.api.endpoint.IUuahEndpoint;
import com.uuah.server.api.transport.IMessageDispatcher;
import com.uuah.server.api.transport.IMessageDispatcherFactory;

/**
 * <code>KeyedPoolMessageDispatcherFactoryAdapter</code> adapts a
 * <code>MessageDispatcherFactory</code> with methods from commons-pool
 * <code>KeyedPoolableObjectFactory</code>. It is only required for dispatcher
 * factories that do not inherit from
 * <code>AbstractMessageDispatcherFactory</code>.
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午03:13:57
 * @version 1.0.0
 */
public class KeyedPoolMessageDispatcherFactoryAdapter implements
		IMessageDispatcherFactory, KeyedPoolableObjectFactory {
	private final IMessageDispatcherFactory factory;

	public KeyedPoolMessageDispatcherFactoryAdapter(
			IMessageDispatcherFactory factory) {
		super();

		if (factory == null) {
			throw new IllegalArgumentException(CoreMessages.objectIsNull(
					"factory").toString());
		}

		this.factory = factory;
	}

	public void activateObject(Object key, Object obj) throws Exception {
		factory.activate((IUuahEndpoint) key, (IMessageDispatcher) obj);
	}

	public void destroyObject(Object key, Object obj) throws Exception {
		factory.destroy((IUuahEndpoint) key, (IMessageDispatcher) obj);
	}

	public Object makeObject(Object key) throws Exception {
		return factory.create((IUuahEndpoint) key);
	}

	public void passivateObject(Object key, Object obj) throws Exception {
		factory.passivate((IUuahEndpoint) key, (IMessageDispatcher) obj);
	}

	public boolean validateObject(Object key, Object obj) {
		return factory.validate((IUuahEndpoint) key, (IMessageDispatcher) obj);
	}

	public boolean isCreateDispatcherPerRequest() {
		return factory.isCreateDispatcherPerRequest();
	}

	public IMessageDispatcher create(IUuahEndpoint endpoint)
			throws AbstractUuahException {
		return factory.create(endpoint);
	}

	public void activate(IUuahEndpoint endpoint, IMessageDispatcher dispatcher)
			throws AbstractUuahException {
		factory.activate(endpoint, dispatcher);
	}

	public void destroy(IUuahEndpoint endpoint, IMessageDispatcher dispatcher) {
		factory.destroy(endpoint, dispatcher);
	}

	public void passivate(IUuahEndpoint endpoint, IMessageDispatcher dispatcher) {
		factory.passivate(endpoint, dispatcher);
	}

	public boolean validate(IUuahEndpoint endpoint, IMessageDispatcher dispatcher) {
		return factory.validate(endpoint, dispatcher);
	}

}
